<?php

class Default_Model_DbMapper_Users extends Wlib_Model_DbMapper_SystemEntity
{
/*    protected $_passwordSalt = null;

    public function setPasswordSalt($passwordSalt) {
        $this->_passwordSalt = $passwordSalt;
    }

    public function getPasswordSalt() {
        return $this->_passwordSalt;
    }*/

/*    public function hashPassword($pass)
    {
        $passSalt = $this->getPasswordSalt();
        return md5($pass . $passSalt);
    }*/

    public function login($login, $password)
    {
        $auth = Zend_Auth::getInstance();

        $authAdapter = new Zend_Auth_Adapter_DbTable(
            $this->getDbTable()->getAdapter(), $this->getDbTable()->info('name'), 'login', 'password'
        );

        //$password = $this->hashPassword($password);
        $authAdapter->setIdentity($login)
                    ->setCredential($password);

        $result = $auth->authenticate($authAdapter);
        if($result->isValid()) {
            $auth->getStorage()->write($authAdapter->getResultRowObject());
            $session = new Zend_Session_Namespace('Zend_Auth');
            $session->setExpirationSeconds(24*3600);
            return true;
        }
        return false;
    }
}

/*    protected $_lengthGeneratedPass = null;

    public function setLengthGeneratedPass($lengthGeneratedPass) {
        $this->_lengthGeneratedPass = $lengthGeneratedPass;
    }

    public function getLengthGeneratedPass() {
        return $this->_lengthGeneratedPass;
    }


    public function setOptions(array $options)
    {
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }

        return $this;
    }*/
/*    public function filterValues($values, $form, $role)
    {
        $userFields = $form->getDisplayGroup(self::USER_GENERAL_BLOCK)->getElements();
        $userFields = array_keys($userFields);

        $userRoles = $this->getUserRoles();
        $roleKeyword = $userRoles[$values['role']];

        $roleValues = array();
        foreach($values as $index => $value) {
            if(in_array($index, $userFields)) {
                $userValues[$index] = $values[$index];
            }

            if($roleKeyword) {
                $prefix = $roleKeyword . '_';
                if(strpos($index, $prefix) === 0) {
                    $roleFiledName = substr($index, strlen($prefix));
                    $roleValues[$roleFiledName] = $value;
                }
            }
        }
        $arResult = array(
            "user" => $userValues,
            "role" => $roleValues
        );

        return $arResult;
    }*/


 /*   public function fetchData($id)
    {
        $data = array();

        $dbRow = $this->fetchDbRowById($id);


        if(!$dbRow) {
            return $data;
        }

        $data = $dbRow->toArray();


        $phonesDbMapper  = new Default_Model_DbMapper_UserPhoneNumbers();
        $data['telephones'] = $phonesDbMapper->fetchValues($id);

        $roleDbMapper = $this->getRoleDbMapper($dbRow['role']);

        if($roleDbMapper) {
            $userRoles = $this->getUserRoles();
            $roleData = $roleDbMapper->fetchDataToUserForm($dbRow['id'], $userRoles[$dbRow['role']] . '_');

            if($dbRow['role'] == self::USER_ROLE_STUDENT_ID) {

                $higherEducationDbMapper = new Default_Model_DbMapper_HigherEducation();
                $additionalEducationDbMapper = new Default_Model_DbMapper_AdditionalEducation();

                $studentHigherEducationDbRowSet = $higherEducationDbMapper->fetchDbRowsetByKey(
                    $roleData['student_id'],
                    'student_id'
                );

                $studentAddtionalEducationDbRowSet = $additionalEducationDbMapper->fetchDbRowsetByKey(
                    $roleData['student_id'],
                    'student_id'
                );
                if($studentHigherEducationDbRowSet) {
                    $data['student_higher_education'] = array('data' => $studentHigherEducationDbRowSet->toArray());
                }
                if($studentHigherEducationDbRowSet) {
                    $data['student_additional_education'] = array('data' => $studentAddtionalEducationDbRowSet->toArray());
                }
            }

            $data = array_merge($data, $roleData);
        }

        return $data;
    }*/

/*    public function generatePassword()
    {
        $useSymList = array(
            'a','b','c','d','e','f',
            'g','h','i','j','k','l',
            'm','n','o','p','r','s',
            't','u','v','x','y','z',
            'A','B','C','D','E','F',
            'G','H','I','J','K','L',
            'M','N','O','P','R','S',
            'T','U','V','X','Y','Z',
            '1','2','3','4','5','6',
            '7','8','9','0','-','_'
        );

        $pass = "";
        $passLenght = $this->getLengthGeneratedPass();

        for($i = 0; $i < $passLenght; $i++)
        {
          $index = rand(0, count($useSymList) - 1);
          $pass .= $useSymList[$index];
        }

        return $pass;
    }*/

/*    public function saveUserPhoto($fileData, $id, $fileName)
    {
        require_once(APPLICATION_PATH . '/../library/WideImage/WideImage.php');

        $options = $this->getPhotoOptions();
        $userPhotoFilePath = $this->getUserPhotoPath() . DIRECTORY_SEPARATOR . $id . DIRECTORY_SEPARATOR;
        $resizeDirPath = $userPhotoFilePath . DIRECTORY_SEPARATOR . 'resize';
        $tmpFile = dirname($fileData['tmp_name']) . DIRECTORY_SEPARATOR . $fileData['name'];
        $fullPath = $userPhotoFilePath . DIRECTORY_SEPARATOR . $fileName;

        $this->createFolder($userPhotoFilePath);
        $isMove = copy($tmpFile, $userPhotoFilePath . DIRECTORY_SEPARATOR . $fileName);
        unset($tmpFile);

        $this->createFolder($resizeDirPath);
        WideImage::load($fullPath)
                ->resize($options['sizeX'], $options['sizeY'])
                ->saveToFile($resizeDirPath . DIRECTORY_SEPARATOR . $fileName);

        if($isMove) {
            return $fileName;
        } else {
            return false;
        }
    }*/

/*     public function seachByLastName($seacrhData, $role)
     {
            $select = $this->select()
                ->where('last_name LIKE ?', $seacrhData . '%');

            if($role) {
                $select->where('role = ?', $role);
            }

            return $this->fetchAll($select);
     }*/

/*    public function getByListId($usersListId)
    {
        if(!is_array($usersListId)) {
            $usersListId = $usersListId->toArray();
        }

        $select = $this->select()
            ->where('id IN (?)', $usersListId);

        return $this->fetchAll($select);
    }*/

    /*    public function getGroupByUserId($id)
        {
            $select = $this->select()->where('default_users.id = ?', $id)
                ->join('default_students', 'default_students.user_id = default_users.id', 'group_id');

            $resultRow = $this->fetchRow($select);
            return $resultRow->group_id;
        }*/
?>